﻿using SecurityApi.Business;
using SecurityApi.DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace SecurityApi.SoapApi
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class SecurityService : ISecurityService
    {

        public List<SecurityApi.Model.Application> GetUserApplicationsByName(string userName, int enterpriseId)
        {
            UserApplicationManager mgr = new UserApplicationManager();
            return mgr.GetUserApplications(userName, enterpriseId);
        }


        public SecurityApi.Model.Application GetUserApplicationsById(int id)
        {
            ApplicationRepository repository = new ApplicationRepository();
            var application = repository.GetApplicationById(id);
            return application;
        }

        public bool VerifyUser(string userName, string password, int enterpriseId)
        {
            UserManager mgr = new UserManager();
            bool bResult = mgr.VerifyPassword(userName, password, enterpriseId);
            return bResult;
        }

        public bool VerifyEnterpriseUser(string userName, string password, string enterpriseName)
        {
            bool bResult = false;
            UserManager mgr = new UserManager();
            EnterpriseManager enterpriseManager = new EnterpriseManager();
            SecurityApi.Model.Enterprise enterprise = enterpriseManager.GetEnterpriseByName(enterpriseName);
            if (null != enterprise)
            {
                bResult = mgr.VerifyPassword(userName, password, enterprise.Id);
            }

            return bResult;

        }

        public SecurityApi.Model.EnterpriseUser GetEnterpriseUser(string userName, int enterpriseId)
        {
            UserManager mgr = new UserManager();
            SecurityApi.Model.EnterpriseUser user = mgr.GetEnterpriseUser(userName, enterpriseId);
            return user;
        }


        public List<SecurityApi.Model.EnterpriseUser> GetEnterpriseUsers(int enterpriseId)
        {
            UserManager mgr = new UserManager();
            List<SecurityApi.Model.EnterpriseUser> users = mgr.GetEnterpriseUsers(enterpriseId);
            return users;
        }



        #region Roles
        public List<SecurityApi.Model.Role> GetApplicationRoles(int applicationId)
        {
            RoleManager mgr = new RoleManager();
            return mgr.GetApplicationRoles(applicationId);
        }

        public List<SecurityApi.Model.Role> GetUserRoles(string userName, int applicationId)
        {
            RoleManager mgr = new RoleManager();
            List<SecurityApi.Model.Role> roles = mgr.GetUserRoles(userName, applicationId);
            return roles;
        }

        public List<SecurityApi.Model.Role> GetUserRolesInEnterprise(int userId, int enterpriseId)
        {
            RoleManager mgr = new RoleManager();
            List<SecurityApi.Model.Role> roles = mgr.GetUserRolesInEnterprise(userId, enterpriseId);
            return roles;
        }

        #endregion


        #region enterprise attribute
        public string GetEnterpriseAttribute(int enterpriseId, string key)
        {
            AttributeManager mgr = new AttributeManager();
            return mgr.GetEnterpriseAttribute(enterpriseId, key);
        }

        #endregion

        #region Application attribute
        public string GetApplicationAttribute(int applicationId, string key)
        {
            AttributeManager mgr = new AttributeManager();
            return mgr.GetApplicationAttribute(applicationId, key);
        }

        #endregion

    }
}
